<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <span>用户名:</span>
          <el-input
            style="width: 300px; margin-left: 10px"
            v-model="input"
            placeholder="请输入用户名"
          ></el-input>
          <span class="ml-10">电话:</span>
          <el-input
            style="width: 300px; margin-left: 10px"
            v-model="input"
            placeholder="请输入电话"
          ></el-input>

          <el-button
            class="ml-10"
            type="primary"
            icon="el-icon-plus"
            @click="showEdit = true"
            >新增</el-button
          >

          <el-table class="mt-20" :data="list">
            <el-table-column prop="id" label="序号" />
            <el-table-column prop="userId" label="用户id" />
            <el-table-column prop="balance" label="积分余额" />
            <el-table-column prop="text" fixed="right" label="操作">
              <template slot-scope="scope">
                <el-button
                  size="small"
                  type="primary"
                  @click="clickInquire(scope.row)"
                  >查询明细</el-button
                >
                <el-button
                  size="small"
                  type="primary"
                  @click="handleEdit(scope.row, scope.$index)"
                  >编辑</el-button
                >
                <el-button
                  size="small"
                  type="danger"
                  @click="handleDelete(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tabs>
      </el-card>
    </div>

    <!-- 查询明细弹框  -->
    <el-dialog title="查询明细" :visible="showDialog" @close="closeDialog">
      <el-form v-loading="loading" label-width="100px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="formData.userId" type="number" disabled />
        </el-form-item>
        <el-form-item label="积分余额" prop="balance">
          <el-input v-model="formData.balance" type="number" disabled />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right">
          <!-- <el-button @click="closeDialog">否</el-button> -->
          <el-button type="primary" @click="closeDialog">确定</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 分页 -->
    <div align="center">
      <!-- <el-pagination
        :current-page.sync="currentPage"
        layout="prev, pager, next, jumper"
        :total="total"
      /> -->
    </div>
    <!-- 编辑/新建弹窗 -->
    <el-dialog
      :visible="showEdit"
      @close="
        showEdit = false
        formData = {}
      "
      label-width="400px"
    >
      <el-form>
        <el-form-item label="balance" prop="balance" v-if="formData.id">
          <el-input v-model="formData.balance" type="number" disabled />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="formData.userId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="积分余额" prop="balance">
          <el-input v-model="formData.balance" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right">
          <el-button @click="showEdit = false">取消</el-button>
          <el-button type="primary" @click="editSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 删除确认框 -->
    <el-dialog title="提示" :visible.sync="deleteVisible" width="30%">
      <span>确定删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteComfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {selectIntegrateBalance,insertIntegrateBalance,updateIntegrateBalance,deleteIntegrateBalance} from '@/api/qiniu'
export default {
  data() {
    return {
      list: [],
      currentPage: 1,
      total: 0,
      pageSize: 10,
      formData: {},
      showEdit: false,
      deleteVisible: false,
      editIndex:null,
      deleteRow:null,
      showDialog:false
    };
  },
  async mounted() {
    // console.log(service)
    // swlService.select();
    this.getList()
  },
  methods: {
    async getList(){
      const res = await selectIntegrateBalance()
      // this.$set(this.list,res)
      res.map((item)=>{
          item.statusName = item.status===1?'正常':'无效'
          return item
      })
      this.list = res;
      this.list = this.list.filter(item => item)
      this.total = res.length
    },
    handleEdit(row,index) {
      console.log(index)
      this.formData = row;
      this.showEdit = true;
      this.editIndex=index
    },
    handleDelete(row) {
      this.deleteRow = row
      this.deleteVisible = true
    },
    // 查询功能
    clickInquire(row){
      this.formData = row
      this.formData.postalcode = row.postalCode
      this.showDialog = true
    },
    async deleteComfirm(){
      const res = await deleteIntegrateBalance({
        id: this.deleteRow.id
      })
      this.$message.success(res)
      this.deleteVisible=false
      this.getList();
    },
    async editSubmit() {
      let res = ''
      if (this.formData.id) {
        //编辑
        this.formData.status=this.formData.status?1:0
        res=await updateIntegrateBalance(this.formData)
      } else {
        //新增
        this.formData.status=this.formData.status?1:0
        res=await insertIntegrateBalance(this.formData)
      }
      this.getList()
      this.formData={}
      this.$message.success(res)
      this.showEdit=false
    },

    closeDialog() {
      this.showDialog = false
    }
  }
};
</script>